﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using COM.Rights.Model;
using COM.Rights.BLL;

public partial class Pages_TUser_Edit : ZMPageEdit<TUser, TUserInfo>
{
    #region 重写方法

    protected override void OnLoading()
    {
        base.OnLoading();

        if (!IsPostBack)
        {
            ZBinder.BindListControl<TRoleInfo>("Rights", Roles, "Name", "GID");
        }
    }
    protected override void OnBinding()
    {
        base.OnBinding();

        TRoleTTTUser bll = new TRoleTTTUser();
        
       
        List<TRoleTTTUserInfo> infos = bll.GetRolsByUser(Id);

        foreach (ListItem item in Roles.Items)
        {
            var lowNums = from n in infos
                          where n.TRole_GID == item.Value
                          select n;
            if (lowNums.ToList<TRoleTTTUserInfo>().Count > 0)
                item.Selected = true;
        }
    }
    protected override void OnBinded()
    {
        base.OnBinded();
        PWD.Visible = false;
        TUserInfo_UID.Disabled = true; checkedIDs.Value = InitFunctionIDs();
    }   

    public string InitFunctionIDs()
    {
        string ids = "";

        foreach (TOUTTTUserInfo info in new TOUTTTUser().GetOUsByUser(Id))
        {
            ids += info.TOU_GID + ",";
        }
        ids.Trim(',');

        return ids;
    }

    protected override void OnAchieving()
    {
        base.OnAchieving();
        if (IsNewModel)
        {
            Model.CreatTime = DateTime.Now;
            Model.Creator = ZSession.ManagerUID;
        }
    }

    protected override void OnAchieved()
    {
        base.OnAchieved();
        if (!IsNewModel)
        {
            Dictionary<string, ModifyInfo> infos = Model.GetModifyInfos();
            infos.Remove("PWD");
            Model.SetModifyInfos(infos);
        }
    }

    protected override void OnSave()
    {
        base.OnSave();
        this.SaveRole();
        this.SaveOU();
    }

    protected void SaveRole()
    {
        List<string> ids = new List<string>();

        List<TRoleTTTUserInfo> infos = new List<TRoleTTTUserInfo>();

        foreach (ListItem item in Roles.Items)
        {
            if (item.Selected)
            {
                TRoleTTTUserInfo info = new TRoleTTTUserInfo();

                info.TRole_GID = item.Value;
                info.TUser_GID = Model.GID;
                infos.Add(info);
            }
        }

        TRoleTTTUser bll = new TRoleTTTUser();
        string filter = "TUser_GID=@TUser_GID";
        List<DBParameter> dbs = new List<DBParameter>();
        dbs.Add(new DBParameter("TUser_GID", Model.GID));
        bll.Delete(filter, dbs);
        bll.Add(infos, "");
    }

    protected void SaveOU()
    {
        base.OnSave();
        string ids = checkedIDs.Value.Trim(',');
        TOUTTTUser bll = new TOUTTTUser();
        bll.SaveOUsByUser(Model.GID, ids);
    }


    #endregion 	重写方法
}